package org.jeecg.modules.asset.chartInfo.controller;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.NoQueryInterceptor;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.asset.chartInfo.entity.dto.ChartAndHireListDTO;
import org.jeecg.modules.asset.chartInfo.service.IChartinfoService;
import org.jeecg.modules.asset.chartInfo.entity.Chartinfo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.asset.fileselfbelong.entity.Fileselfbelong;
import org.jeecg.modules.asset.fileselfbelong.service.IFileselfbelongService;
import org.jeecg.modules.asset.hirelist.entity.Hirelist;
import org.jeecg.modules.asset.hirelist.service.IHirelistService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.asset.roomchangehirelog.entity.Roomchangehirelog;
import org.jeecg.modules.asset.roomchangehirelog.service.IRoomchangehirelogService;
import org.jeecg.modules.asset.roomchartlog.entity.Roomchartlog;
import org.jeecg.modules.asset.roomchartlog.service.IRoomchartlogService;
import org.jeecg.modules.asset.roomexitlog.entity.Roomexitlog;
import org.jeecg.modules.asset.roomexitlog.service.IRoomexitlogService;
import org.jeecg.modules.asset.roominfo.entity.Roominfo;
import org.jeecg.modules.asset.roominfo.service.IRoominfoService;
import org.jeecg.modules.asset.roomlog.entity.Roomlog;
import org.jeecg.modules.asset.roomlog.service.IRoomlogService;
import org.jeecg.modules.asset.roomreletlog.entity.Roomreletlog;
import org.jeecg.modules.asset.roomreletlog.service.IRoomreletlogService;
import org.jeecg.modules.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Description: ChartInfo
 * @Author: jeecg-boot
 * @Date:   2020-03-30
 * @Version: V1.0
 */
@RestController
@RequestMapping("/chartinfo/chartinfo")
@Slf4j
public class ChartinfoController extends JeecgController<Chartinfo, IChartinfoService> {

	@Autowired
	private IChartinfoService chartinfoService;
	@Autowired
	private IHirelistService hirelistService;
	@Autowired
	private IRoominfoService roominfoService;
	@Autowired
	private IRoomexitlogService roomexitlogService;
	@Autowired
	private IFileselfbelongService fileselfbelongService;
	@Autowired
	private IRoomlogService roomlogService;
	@Autowired
	private IRoomchartlogService roomchartlogService;
	@Autowired
	private IRoomreletlogService roomreletlogService;
	@Autowired
	private IRoomchangehirelogService roomchangehirelogService;

	/**
	 *  返回所有的经营项目
	 * @return
	 */
	@NoQueryInterceptor
	@PostMapping(value = "/fareItem")
	public Result<?> fareItem() {
		Result<Object> result = new Result<>();
		List<String> list = chartinfoService.findAllFareItem();
		result.setResult(list);
		return result;
	}

	/**
	 * 出租
	 * @param id	资产id
	 * @param chartinfo	输入的合同信息
	 * @param hirelist	输入的租金信息
	 * @return
	 * @throws ParseException
	 */
	 @PostMapping(value = "/rent")
	 public Result<?> rent(@RequestBody String id,@RequestBody Chartinfo chartinfo, @RequestBody Hirelist hirelist) throws ParseException {
	 	//判断是否已出租或不可出租
		 Roominfo roominfo = roominfoService.getById(id);
		 String guid = roominfo.getGuid();	//资产guid
		 if("已出租".equals(roominfo.getState())){
			 return Result.ok("房屋已出租，暂时不能出租！");
		 }
		 if("不可出租".equals(roominfo.getState()) || "不可出租2".equals(roominfo.getState())){
			 return Result.ok("房屋不可出租，暂时不能出租！");
		 }

		 String chartGuid = UUID.randomUUID().toString();	//新生成的合同guid
		 String hireGuid = chartGuid;	//新生成的合同的hireguid  和guid公用一个UUID
		 chartinfo.setGuid(chartGuid);
		 chartinfo.setHireguid(hireGuid);
		 chartinfo.setIshistory("0");	//合同是否到期 0为未到期，0以为的所有值为到期
		 chartinfoService.save(chartinfo);

		 //根据guid修改资产表中的chartGuid
		 Roominfo roominfo1 = new Roominfo();
		 roominfo1.setId(id);		//房屋id
		 roominfo1.setChartguid(chartGuid);
		 roominfo1.setState("已出租");		//房屋状态
		 roominfoService.updateById(roominfo1);

		 //将租金信息添加到hirelist
		 SimpleDateFormat slf = new SimpleDateFormat("yyyy-MM");
		 Date chartbegindate = chartinfo.getChartbegindate();
		 Date chartenddate = chartinfo.getChartenddate();
		 //根据当天得到下一个月这天的日期
		 Calendar c1 = Calendar.getInstance();
		 c1.setTime(chartenddate);
		 int year = c1.get(Calendar.YEAR);
		 int month = c1.get(Calendar.MONTH);
		 int today = c1.get(Calendar.DAY_OF_MONTH);
		 String last = "";
		 if(month == 12){
		 	last = (year+1) + "-" + 1 + "-" + today;
		 }else {
			last = year + "-" + (month+1) + "-"	+ today;	 	
		 }
		 List<String> monthBetween = TimeUtils.getMonthBetween(chartbegindate, chartenddate);
		 List<String> dateStr = new ArrayList<>();
		 for (String s : monthBetween) {
			 s = s + today;
			 dateStr.add(s);
		 }
		 dateStr.add(last);
		 SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
		 for (int i = 0; i < (dateStr.size() - 1); i++) {
			 hirelist.setHiredate(monthBetween.get(i));
			 String begin = dateStr.get(i);		//开始时间
			 String end = dateStr.get(i + 1);	//结束时间
			 //验证日期是否为正确日期 不正常则取为当月的最后一天
			 begin = TimeUtils.correctStr(begin);
			 end = TimeUtils.correctStr(end);
			 hirelist.setBeginDate(sd.parse(begin));
			 hirelist.setEndDate(sd.parse(end));
			 hirelist.setHireguid(hireGuid);	//插入hireguid
			 hirelist.setGuid(UUID.randomUUID().toString());	//新生成的guid
			 //循环插入到表
			 hirelistService.save(hirelist);
		 }

		 //将信息存入到RoomChartLog
		 Roomchartlog roomchartlog = new Roomchartlog();
		 String roomchartlogGUID = UUID.randomUUID().toString();
		 roomchartlog.setGuid(roomchartlogGUID);
		 roomchartlog.setRoomaddress(roominfo.getAddress());
		 roomchartlog.setRoomcount(roominfo.getRoomcount());
		 roomchartlog.setCharter(chartinfo.getCharter());
		 roomchartlog.setCharttype(chartinfo.getCharttype());
		 roomchartlog.setHire(chartinfo.getHire());
		 roomchartlog.setChartcriterion(chartinfo.getChartcriterion());
		 roomchartlog.setArea(chartinfo.getArea());
		 roomchartlog.setContractno(chartinfo.getContractno());
		 roomchartlog.setConcludedate(chartinfo.getConcludedate());
		 roomchartlog.setChartbegindate(chartbegindate);
		 roomchartlog.setChartenddate(chartenddate);
		 roomchartlog.setRegion(chartinfo.getRegion());
		 roomchartlog.setManageregion(chartinfo.getManageregion());
		 roomchartlog.setRoomproperty(chartinfo.getRoomproperty());
		 roomchartlog.setUseful(chartinfo.getUseful());
		 roomchartlog.setOriginalunit(chartinfo.getOriginalunit());
		 roomchartlog.setBelongunit(chartinfo.getBelongunit());
		 roomchartlog.setOperator("管理员");	//需要修改
		 roomchartlog.setOptdate(new Date());
		 roomchartlogService.save(roomchartlog);
		 //将信息存入到RoomLog
		 Roomlog roomlog = new Roomlog();
		 roomlog.setGuid(UUID.randomUUID().toString());
		 roomlog.setRoomguid(roominfo.getGuid());
		 roomlog.setActiondate(chartinfo.getChartbegindate());	//合同开始的日期
		 roomlog.setActiontype("出租");	//动作
		 roomlog.setInstruction("出租类型:"+chartinfo.getCharttype()+" "+"承租人("+chartinfo.getCharter()+")，"+
				 "合同期限("+slf.format(chartbegindate)+"至"+slf.format(chartenddate)+")，"+"月租金:"+chartinfo.getHire()
				 +"元，"+"租赁标准:"+chartinfo.getChartcriterion()+"元/㎡，"+"缴纳保证金:"+chartinfo.getAssureamount()+
				 "元."+"承租房屋("+roominfo.getAddress()+")");
		 roomlog.setOperator("管理员");		//还未获取真正的管理员
		 roomlog.setLogdate(new Date());
		 Fileselfbelong fileSelfBelong =  fileselfbelongService.findByRoomGUID(roominfo.getGuid());
		 if(fileSelfBelong == null){
			 roomlog.setIsfile("无");	//有无图片
		 }else {
			 roomlog.setIsfile("有");	//有无图片
		 }
		 roomlog.setFileparentguid("");	//不知哪里取
		 roomlog.setParentguid(roomchartlogGUID);
		 //		 roomlog						//这里还需要一条chartGUID 但是数据库缺少此字段
		 roomlogService.save(roomlog);


		 return Result.ok("出租成功！");
	 }

	/**
	 * 返回该资产的合同信息
	 * @param id	资产id
	 * @return
	 */
	@PostMapping(value = "/returnMessage")
	public Result<?> returnMessage(@RequestBody String id) {
		//判断是否空置或不可出租
		Roominfo roominfo = roominfoService.getById(id);
		String guid = roominfo.getGuid();
		if("空置".equals(roominfo.getState())){
			return Result.ok("房屋已出租，暂时不能出租！");
		}
		if("不可出租".equals(roominfo.getState()) || "不可出租2".equals(roominfo.getState())){
			return Result.ok("房屋不可出租，暂时不能出租！");
		}
		//判断已出租资产是否结清租金，未结清，无法续租
		Chartinfo chartinfo = chartinfoService.getByGuid(roominfo.getChartguid());
		String hireguid = chartinfo.getHireguid();
		List<Hirelist> list = hirelistService.getByHireGuid(hireguid);
		for (Hirelist hirelist : list) {
			String state = hirelist.getState();
			if("未交".equals(state)){
				return Result.ok("还有租金未交，不能续租！");
			}
		}
		ChartAndHireListDTO dto = new ChartAndHireListDTO();
		dto.setChartinfo(chartinfo);
		dto.setHirelists(list);
		return Result.ok(dto);
	}

	/**
	 * 续租
	 * @param id	资产id
	 * @param chartinfo	输入的合同信息
	 * @param hirelist	输入的租金信息
	 * @return
	 * @throws ParseException
	 */
	@PostMapping(value = "/relet")
	public Result<?> relet(@RequestBody String id,@RequestBody Chartinfo chartinfo, @RequestBody Hirelist hirelist) throws ParseException {

		//得到原合同的guid 作为新合同的 hireguid
		Roominfo roominfo = roominfoService.getById(id);
		Chartinfo chartinfoOld = chartinfoService.getByGuid(roominfo.getChartguid());
		String hireGuid = chartinfoOld.getGuid();	//新合同的hireguid

		String chartGuid = UUID.randomUUID().toString();	//新生成的合同guid
		chartinfo.setGuid(chartGuid);
		chartinfo.setHireguid(hireGuid);
		chartinfo.setIshistory("0");	//合同是否到期 0为未到期，0以为的所有值为到期
		chartinfoService.save(chartinfo);

		//根据guid修改资产表中的chartGuid
		Roominfo roominfo1 = new Roominfo();
		roominfo1.setId(id);		//房屋id
		roominfo1.setChartguid(chartGuid);
		roominfo1.setState("已出租");		//房屋状态
		roominfoService.updateById(roominfo1);

		Date chartbegindate = chartinfo.getChartbegindate();
		Date chartenddate = chartinfo.getChartenddate();

		//根据当天得到下一个月这天的日期
		Calendar c1 = Calendar.getInstance();
		c1.setTime(chartenddate);
		int year = c1.get(Calendar.YEAR);
		int month = c1.get(Calendar.MONTH);
		int today = c1.get(Calendar.DAY_OF_MONTH);
		String last = "";
		if(month == 12){
			last = (year+1) + "-" + 1 + "-" + today;
		}else {
			last = year + "-" + (month+1) + "-"	+ today;
		}
		List<String> monthBetween = TimeUtils.getMonthBetween(chartbegindate, chartenddate);
		List<String> dateStr = new ArrayList<>();
		for (String s : monthBetween) {
			s = s + today;
			dateStr.add(s);
		}
		dateStr.add(last);
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
		for (int i = 0; i < (dateStr.size() - 1); i++) {
			hirelist.setHiredate(monthBetween.get(i));
			String begin = dateStr.get(i);		//开始时间
			String end = dateStr.get(i + 1);	//结束时间
			//验证日期是否为正确日期 不正常则取为当月的最后一天
			begin = TimeUtils.correctStr(begin);
			end = TimeUtils.correctStr(end);
			hirelist.setBeginDate(sd.parse(begin));
			hirelist.setEndDate(sd.parse(end));
			hirelist.setHireguid(hireGuid);	//插入hireguid
			hirelist.setGuid(UUID.randomUUID().toString());	//新生成的guid
			//循环插入到表
			hirelistService.save(hirelist);
		}
		//将信息存入到RoomReletLog
		Roomreletlog roomreletlog = new Roomreletlog();
		roomreletlog.setGuid(UUID.randomUUID().toString());
		roomreletlog.setRoomaddress(chartinfo.getRoomaddress());
		roomreletlog.setRoomcount(roominfo.getRoomcount());
		roomreletlog.setCharter(chartinfo.getCharter());
		roomreletlog.setArea(chartinfo.getArea());
//		List<String> monthBetween1 = TimeUtils.getMonthBetween(chartinfo.getChartbegindate(), chartinfo.getChartenddate());
		roomreletlog.setYears(monthBetween.size()/12);		//租期

		//原来的合同信息
		roomreletlog.setOhire(chartinfoOld.getHire());	//  oldHire
		roomreletlog.setOchartcriterion(chartinfoOld.getChartcriterion());	//原来的租赁标准
		roomreletlog.setOcontractno(chartinfoOld.getContractno());
		roomreletlog.setOconcludedate(chartinfoOld.getConcludedate());
		roomreletlog.setOchartbegindate(chartinfoOld.getChartbegindate());
		roomreletlog.setOchartenddate(chartinfoOld.getChartenddate());
		//新的合同信息
		roomreletlog.setNhire(chartinfo.getHire());	//  oldHire
		roomreletlog.setNchartcriterion(chartinfo.getChartcriterion());	//原来的租赁标准
		roomreletlog.setNcontractno(chartinfo.getContractno());
		roomreletlog.setNconcludedate(chartinfo.getConcludedate());
		roomreletlog.setNchartbegindate(chartinfo.getChartbegindate());
		roomreletlog.setNchartenddate(chartinfo.getChartenddate());

		roomreletlog.setRegion(chartinfo.getRegion());
		roomreletlog.setManageregion(chartinfo.getManageregion());
		roomreletlog.setRoomproperty(chartinfo.getRoomproperty());
		roomreletlog.setUseful(chartinfo.getUseful());
		roomreletlog.setOriginalunit(chartinfo.getOriginalunit());
		roomreletlog.setBelongunit(chartinfo.getBelongunit());
		roomreletlog.setOperator(chartinfo.getOperator());
		roomreletlog.setOptdate(new Date());
		roomreletlogService.save(roomreletlog);

		//将信息存入到RoomLog
		SimpleDateFormat slf = new SimpleDateFormat("yyyy-MM");
		Roomlog roomlog = new Roomlog();
		roomlog.setGuid(UUID.randomUUID().toString());
		roomlog.setRoomguid(roominfo.getGuid());
		roomlog.setActiondate(chartinfo.getChartbegindate());	//合同开始的日期
		roomlog.setActiontype("出租");	//动作
		roomlog.setInstruction("出租类型:"+chartinfo.getCharttype()+" "+"承租人("+chartinfo.getCharter()+")，"+
				"合同期限("+slf.format(chartbegindate)+"至"+slf.format(chartenddate)+")，"+"月租金:"+chartinfo.getHire()
				+"元，"+"租赁标准:"+chartinfo.getChartcriterion()+"元/㎡，"+"缴纳保证金:"+chartinfo.getAssureamount()+
				"元."+"承租房屋("+roominfo.getAddress()+")");
		roomlog.setOperator("管理员");		//还未获取真正的管理员
		roomlog.setLogdate(new Date());
		Fileselfbelong fileSelfBelong =  fileselfbelongService.findByRoomGUID(roominfo.getGuid());
		if(fileSelfBelong == null){
			roomlog.setIsfile("无");	//有无图片
		}else {
			roomlog.setIsfile("有");	//有无图片
		}
		roomlog.setFileparentguid("");	//不知哪里取
		roomlog.setParentguid("");	//来自roomchartlog de roomchartlogGUID
		//		 roomlog						//这里还需要一条chartGUID 但是数据库缺少此字段
		roomlogService.save(roomlog);


		return Result.ok("续租成功！");
	}


	/**
	 * 返回退租信息 退租界面只做展示不做修改
	 * @param id	资产id
	 * @return
	 * @throws ParseException
	 */
	@PostMapping(value = "/throwLease")
	public Result<?> throwLease(@RequestBody String id){

		Roominfo roominfo = roominfoService.getById(id);
		//判断是否为空置或不可出租
		if("空置".equals(roominfo.getState())){
			return Result.ok("房屋已出租，暂时不能出租！");
		}
		if("不可出租".equals(roominfo.getState()) || "不可出租2".equals(roominfo.getState())){
			return Result.ok("房屋不可出租，暂时不能出租！");
		}

		//只展示未交
		Chartinfo chartinfo = chartinfoService.getByGuid(roominfo.getChartguid());
		List<Hirelist> hirelists = hirelistService.getWeiJiaoByHireGuid(chartinfo.getHireguid());

		ChartAndHireListDTO dto = new ChartAndHireListDTO();
		dto.setChartinfo(chartinfo);
		dto.setHirelists(hirelists);

		return Result.ok(dto);
	}


	/**
	 * 退租修改了chartinfo表中的IsHistory状态，删除了roominfo表中的chartGUID，roomexitlog表中的添加了一行新的数据
	 * @return
	 * @throws ParseException
	 */
	@PostMapping(value = "/noRent")
	public Result<?> noRent(@RequestBody Integer id,@RequestBody Chartinfo chartinfo,
							@RequestBody String exitType,@RequestBody Date date){
		Roominfo roominfo1 = roominfoService.getById(id);
		//修改chartinfo表中的IsHistory状态,及state为空置
		String chartguid = roominfo1.getChartguid();
		chartinfoService.updateIsHistoryByGuid(chartguid);
		//roomexitlog表中的添加了一行新的数据
		Roomexitlog roomexitlog = new Roomexitlog();
		roomexitlog.setGuid(UUID.randomUUID().toString());	//guid
		roomexitlog.setRoomaddress(roominfo1.getAddress());	//地址
		roomexitlog.setRoomcount(roominfo1.getRoomcount());
		roomexitlog.setCharter(chartinfo.getCharter());		//承租人
		roomexitlog.setExittype(exitType);	//退租类型
		roomexitlog.setHire(chartinfo.getHire());	//月租金
		roomexitlog.setChartcriterion(chartinfo.getChartcriterion());
		roomexitlog.setArea(chartinfo.getArea());
		roomexitlog.setConcludedate(chartinfo.getConcludedate());	//签订日期
		roomexitlog.setRegion(chartinfo.getRegion());
		roomexitlog.setManageregion(chartinfo.getManageregion());
		roomexitlog.setRoomproperty(roominfo1.getRoomproperty());
		roomexitlog.setUseful(chartinfo.getUseful());
		roomexitlog.setOriginalunit(chartinfo.getOriginalunit());
		roomexitlog.setBelongunit(chartinfo.getBelongunit());
		roomexitlog.setOptdate(date);
		roomexitlog.setOperator("");			//管理员未查
		roomexitlogService.save(roomexitlog);

		//将信息存入到RoomLog
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
		Roomlog roomlog = new Roomlog();
		roomlog.setGuid(UUID.randomUUID().toString());
		roomlog.setRoomguid(roominfo1.getGuid());
		Date date1 = new Date();
		roomlog.setActiondate(date1);	//操作日期
		roomlog.setActiontype("退租");	//动作
		roomlog.setInstruction("承租人("+chartinfo.getCharter()+")"+"于"+sd.format(new Date())+"退租，"+"退租类型:"+
						exitType
				);
		roomlog.setOperator("管理员");		//还未获取真正的管理员
		roomlog.setLogdate(date1);
		Fileselfbelong fileSelfBelong =  fileselfbelongService.findByRoomGUID(roominfo1.getGuid());
		if(fileSelfBelong == null){
			roomlog.setIsfile("无");	//有无图片
		}else {
			roomlog.setIsfile("有");	//有无图片
		}
		roomlog.setFileparentguid("");	//不知哪里取
		roomlog.setParentguid("");	//来自roomchartlog de roomchartlogGUID
		//		 roomlog						//这里还需要一条chartGUID 但是数据库缺少此字段
		roomlogService.save(roomlog);


		//删除roominfo表中的chartGUID，设置状态为空置			可能会删多条的
		//根据chartGuid查询出租的房屋（可能会有多个）
		List<Roominfo> list = roominfoService.getByChartGuid(chartguid);
		for (Roominfo roominfo2 : list) {
			roominfo2.setChartguid(null);
			roominfo2.setState("空置");
			roominfoService.updateById(roominfo2);
		}
		return Result.ok();
	}


	/**
	 * 返回所有合同信息
	 * @return
	 */
	@PostMapping(value = "/returnRent")
	public Result<?> returnRent(@RequestBody Integer id){
		Roominfo roominfo1 = roominfoService.getById(id);
		//判断是否为空置或不可出租
		if("空置".equals(roominfo1.getState())){
			return Result.ok("房屋已出租，暂时不能出租！");
		}
		if("不可出租".equals(roominfo1.getState()) || "不可出租2".equals(roominfo1.getState())){
			return Result.ok("房屋不可出租，暂时不能出租！");
		}
		Roominfo roominfo = roominfoService.getById(id);
		Chartinfo chartinfo = chartinfoService.getByGuid(roominfo.getChartguid());
		return Result.ok(chartinfo);
	}

	/**
	 * 调租		只能修改租金，租金会以租金/月数的方式修改租赁标准
	 * chartinfo 含修改的租金和租赁标准
	 * id roomInfo  的id
	 * @return
	 */
	@PostMapping(value = "/updateRent")
	public Result<?> updateRent(@RequestBody Chartinfo chartinfo,@RequestBody Integer id){

		//查询roominfo
		Roominfo roominfo = roominfoService.getById(id);

		//添加数据到roomLog
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
		Roomlog roomlog = new Roomlog();
		roomlog.setGuid(UUID.randomUUID().toString());
		roomlog.setRoomguid(roominfo.getGuid());
		Date date1 = new Date();
		roomlog.setActiondate(date1);	//操作日期
		roomlog.setActiontype("调租");	//动作
		Chartinfo chartinfo1 = chartinfoService.getByGuid(chartinfo.getGuid());	//含原租金的chartinfo1
		roomlog.setInstruction("月租金从"+ chartinfo1.getHire()+"元调至"+chartinfo.getHire()+"元,租赁标准从"+
				chartinfo1.getChartcriterion()+"元/㎡调至"+chartinfo.getChartcriterion()+"元/㎡"
		);
		roomlog.setOperator("管理员");		//还未获取真正的管理员
		roomlog.setLogdate(date1);
		Fileselfbelong fileSelfBelong =  fileselfbelongService.findByRoomGUID(roominfo.getGuid());
		if(fileSelfBelong == null){
			roomlog.setIsfile("无");	//有无图片
		}else {
			roomlog.setIsfile("有");	//有无图片
		}
		roomlog.setFileparentguid("");	//不知哪里取
		roomlog.setParentguid("");	//来自roomchartlog de roomchartlogGUID
		//		 roomlog						//这里还需要一条chartGUID 但是数据库缺少此字段
		roomlogService.save(roomlog);
		//添加数据到RoomChangeHireLog
		Roomchangehirelog roomchangehirelog = new Roomchangehirelog();
		roomchangehirelog.setGuid(UUID.randomUUID().toString());
		roomchangehirelog.setRoomaddress(chartinfo.getRoomaddress());
		roomchangehirelog.setRoomcount(roominfo.getRoomcount());
		roomchangehirelog.setCharter(chartinfo.getCharter());
		roomchangehirelog.setOhire(chartinfo.getHire());	//  oldHire
		roomchangehirelog.setOchartcriterion(chartinfo.getChartcriterion());	//原来的租赁标准
		roomchangehirelog.setArea(chartinfo.getArea());
		roomchangehirelog.setNhire(chartinfo.getHire());	//  oldHire
		roomchangehirelog.setNchartcriterion(chartinfo.getChartcriterion());	//原来的租赁标准
		roomchangehirelog.setConcludedate(chartinfo.getConcludedate());
		roomchangehirelog.setRegion(chartinfo.getRegion());
		roomchangehirelog.setManageregion(chartinfo.getManageregion());
		roomchangehirelog.setRoomproperty(chartinfo.getRoomproperty());
		roomchangehirelog.setUseful(chartinfo.getUseful());
		roomchangehirelog.setOriginalunit(chartinfo.getOriginalunit());
		roomchangehirelog.setBelongunit(chartinfo.getBelongunit());
		roomchangehirelog.setOperator(chartinfo.getOperator());
		roomchangehirelog.setOptdate(new Date());
		roomchangehirelogService.save(roomchangehirelog);

		//修改chartinfo Update ChartInfo Set Hire=100, ChartCriterion=1.15 Where GUID='{1E792812-0343-4E84-89CF-98097AF337A9}'
		chartinfoService.updateByGuid(chartinfo);

		return Result.ok();
	}





	/**
	 *   模糊查询
	 * @param str
	 * @return
	 */
	@PostMapping(value = "/fuzzySearch")
	public Result<?> fuzzySearch(String str,
								 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {

		//分页方法
		Page<Chartinfo> page = new Page<Chartinfo>(pageNo, pageSize);
		IPage<Chartinfo> pageList = chartinfoService.getByFuzzy(page,str);
		return Result.ok(pageList);
	}


	/**
	 * 分页列表查询
	 *
	 * @param chartinfo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Chartinfo chartinfo,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<Chartinfo> queryWrapper = QueryGenerator.initQueryWrapper(chartinfo, req.getParameterMap());
		Page<Chartinfo> page = new Page<Chartinfo>(pageNo, pageSize);
		IPage<Chartinfo> pageList = chartinfoService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param chartinfo
	 * @return
	 */
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Chartinfo chartinfo) {
		chartinfoService.save(chartinfo);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param chartinfo
	 * @return
	 */
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody Chartinfo chartinfo) {
		chartinfoService.updateById(chartinfo);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		chartinfoService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.chartinfoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Chartinfo chartinfo = chartinfoService.getById(id);
		if(chartinfo==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(chartinfo);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param chartinfo
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Chartinfo chartinfo) {
        return super.exportXls(request, chartinfo, Chartinfo.class, "ChartInfo");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Chartinfo.class);
    }



}
